<template>
    <el-card class="item">
        <template #header>
            <div class="card-header">
                <span>土壤湿度</span>
            </div>
        </template>
        <div ref="chartRef" style="width: 100%;height: 250px;"></div>
    </el-card>      
</template>
   
<script setup>   
import { onMounted, ref, watch, onUnmounted } from 'vue';   
import * as echarts from 'echarts';
   
const chartRef = ref(null);   
let myChart = null;
   
// 初始化图表   
const initChart = () => {
    if (!chartRef.value) return;
    
    // 确保销毁旧实例
    if (myChart) {
        myChart.dispose();
    }
    
    myChart = echarts.init(chartRef.value);
    
    const option = {
        xAxis: {
            type: 'category',
            data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
            axisLabel: { color: '#666' }
        },
        yAxis: {
            type: 'value',
            axisLabel: { color: '#666' }
        },
        series: [{
            type: 'line',
            data: props.soil || [],
            smooth: true,
            symbol: 'circle',
            symbolSize: 8,
            itemStyle: { color: '#409EFF' },
            lineStyle: { width: 3, color: '#409EFF' },
            areaStyle: {
                color: {
                    type: 'linear',
                    x: 0, y: 0, x2: 0, y2: 1,
                    colorStops: [
                        { offset: 0, color: 'rgba(64,158,255,0.6)' },
                        { offset: 1, color: 'rgba(64,158,255,0.1)' }
                    ]
                }
            }
        }],
        tooltip: {
            trigger: 'axis',
            axisPointer: { type: 'cross' }
        }
    };
    
    myChart.setOption(option);   
}
   
// 响应式更新图表数据   
const props = defineProps({ soil: Array })   
watch(() => props.soil, (newVal) => {
    if (myChart && newVal) {
        myChart.setOption({
            series: [{ data: newVal }]
        });
    }   
});
   
// 窗口缩放时自适应   
const handleResize = () => myChart?.resize();   
onMounted(() => {
    initChart();
    window.addEventListener('resize', handleResize);   
});
   
// 组件卸载时清理   
onUnmounted(() => {
    if (myChart) {
        myChart.dispose();
        myChart = null;
    }
    window.removeEventListener('resize', handleResize);   
});   
</script>

<!-- <template>
    <el-card class="item">
        <template #header>
            <div class="card-header">
                <span>土壤湿度</span>
            </div>
        </template>
        <div ref="chartRef" style="width: 100%;height: 250px;"></div>
    </el-card>   
</template> -->
   
<!-- <script setup>   
import { onMounted, watchEffect, ref, onUnmounted } from 'vue';   
import * as echarts from 'echarts';
   
const chartRef = ref(null);   
let myChart = null;
   
const initChart = () => {
    if (!myChart) {
        myChart = echarts.init(chartRef.value);
    }
    
    const option = {
        xAxis: {
            type: 'category',
            data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
            axisLabel: {
                color: '#666'
            }
        },
        yAxis: {
            type: 'value',
            axisLabel: {
                color: '#666'
            }
        },
        series: [{
    type: 'line',
    data: props.soil,
    smooth: true,
    symbol: 'circle',
    symbolSize: 8,
    itemStyle: {
        color: '#409EFF' // 数据点改为蓝色
    },
    lineStyle: {
        width: 3,
        color: '#409EFF' // 线条改为蓝色
    },
    areaStyle: {
        color: {
            type: 'linear',
            x: 0, y: 0, x2: 0, y2: 1,
            colorStops: [{
                offset: 0, 
                color: 'rgba(64,158,255,0.6)' // 浅蓝渐变
            }, {
                offset: 1,
                color: 'rgba(64,158,255,0.1)' // 透明蓝渐变
            }]
        }
    }   
}],
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross' // 更精准的数值指示
        }
        }
    };
    myChart.setOption(option);   
}
   
const props = defineProps({
    soil: Array   
})
   
watchEffect(() => {
    if (myChart && props.soil) {
        myChart.dispose();
        myChart = null;
        initChart();
    }   
});
   
onMounted(() => {
    initChart();   
});
   
onUnmounted(() => {
    if (myChart) {
        myChart.dispose();
    }   
});    
</script> -->